﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


using Jungle.Code.Utils;
using Jungle.Rent;

namespace Jungle.Alex.WebControls.RndLists
{
    public class TabList : WebControl
    {
        private string typeR;
        public string TypeR { get { return typeR; } set { typeR = value; } }

        //--------------------------------------------------------------------
        protected override void RenderContents(HtmlTextWriter writer)
        {
            Offer offs = new Offer();
            offs.GetRnd(typeR);
            foreach (Offer o in offs.OfferList)
            {
                string imgUrl = UrlHelper.GetSiteUrl();
                int topMove = 0;
                if (o.Photo.Length > 10)
                {
                    string ff = "";
                    if (o.Photo.IndexOf(",") != -1)
                    {
                        ff = o.Photo.Substring(0, o.Photo.IndexOf(","));
                        imgUrl += ff;
                    }
                    else
                    {
                        ff = o.Photo;
                        imgUrl += ff;
                    }
                    System.Drawing.Image img = System.Drawing.Image.FromFile(Context.Server.MapPath(ff));
                    if (img.Height > 69) topMove = (-1) * (img.Height - 69) / 2;
                }
                else
                    imgUrl += "/App_Themes/alex/img/el/nophoto_mini.jpg";
                writer.AddAttribute("class", "preItem");
                writer.RenderBeginTag(HtmlTextWriterTag.Div);

                writer.AddAttribute("class", "bg_preImg");
                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                writer.AddAttribute("class", "preImg");
                writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundImage, imgUrl);
                writer.AddStyleAttribute("background-position", "0px " + topMove.ToString() + "px");
                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                writer.RenderEndTag();
                writer.RenderEndTag();

                writer.AddAttribute("class", "pre_descr");
                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                writer.Write(GetShortDescription(o));
                writer.RenderEndTag();

                writer.RenderEndTag();
            }
            base.RenderContents(writer);
        }
        //-----------------------------------------------------------------------------------------------
        public string GetShortDescription(Offer o)
        {
            string result = "";
            string a0 = "";

            if (o.OfferType) a0 = "<b>Продается "; else a0 = "<b>Сдаеться в аренду ";
            result = a0 + Offer.ddl_UnderType(o.ObjectType).Items.FindByValue(o.UnderTypeId.ToString()).Text + "</b>";

                 result += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\""+UrlHelper.GetSiteUrl()+ "/ru/offers.aspx?cmd=show&id="+ o.OfferId.ToString() + "\" style=\"color: #666; font-size: 80%; font-weight: bold;\">[Подробнее]</a>";

                 result += "<h1>";
            if(o.CityId > 0)
            {
                result += "<br/>" + Offer.ddl_City().Items.FindByValue(o.CityId.ToString()).Text + " " + o.District + " " + o.Street + " " + o.HomeNumber;
            }
            result += "<br/>Объявление от: " + o.PubDate;
            if (o.AutorType == 0) result += ", автор объявления: хозяин";
            else result += ", автор объявления: агенство";
            result += "</h1>";

            int lenght = 160;
            if (o.Description.Length < lenght) lenght = o.Description.Length;
            result += o.Description.Substring(0, lenght);
            return result;
        }
        //-----------------------------------------------------------------------------------------------
        public HtmlTextWriter PrintImage(HtmlTextWriter writer, string imgUrl)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Src, UrlHelper.GetSiteUrl() + imgUrl);
            writer.AddAttribute(HtmlTextWriterAttribute.Align, "absmiddle");
            writer.RenderBeginTag(HtmlTextWriterTag.Img);
            writer.RenderEndTag();
            return writer;
        }
    }
}
